#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <limits>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>

using namespace std;

typedef long long int64;
typedef unsigned long long qword;

typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<vii> viii;
typedef vector<pii> vpii;

typedef pair<int64,int64> pii64;
typedef vector<int64> vi64;
typedef vector<vi64> vii64;
typedef vector<vii64> viii64;
typedef vector<pii64> vpii64;

typedef vector<string> vs;

const int dx[]={0,-1,0,1,-1,1, 1,-1};
const int dy[]={1,0,-1,0, 1,1,-1,-1};
const double eps=1e-8;

#define fori(i,a,b) for(int i(a),_b(b);i<=_b;++i)
#define ford(i,a,b) for(int i(a),_b(b);i>=_b;--i)
#define forn(i,n) fori(i,0,n-1)
#define fora(i,a) fori(i,0,a.size()-1)
#define fore(it,c) for(typeof((c).begin()) it=(c).begin();it!=(c).end();++it)
#define all(a) a.begin(),a.end()
#define mp make_pair
#define maximum(a) (*max_element(all(a)))
#define minimum(a) (*min_element(all(a)))
#define findx(a,x) (find(all(a),x)-a.begin())
#define two(X) (1LL<<(X))
#define contain(S,X) (((S)&two(X))!=0)
#define sqr(a) (a)*(a)
#define nmax 1010
#define mmax 200100
#define kmax 11
#define modulo 1000000007
#define maxc 999999999

int exchange(char z)
{
	if ('0' <= z && z <= '9')
		return z-48;
	if ('A' <= z && z <= 'Z')
		return z-65+10;
	if ('a' <= z && z <= 'z')
		return z-97+36;
	return 9999;
}

class MysteriousRestaurant {
public:
	int maxDays(vector <string> prices, int budget) {
		int n = prices.size();
		int m = prices[0].size();
		for(int ans = 0; ans < n; ans++)
		{
			int bud = budget;
			for(int i = 0; i < 7; i++)
			{
				int minp = 99999999;
				for(int j = 0; j < m; j++)
				{
					int price = 0;
					for(int k = i; k <= ans; k+=7)
						price+=exchange(prices[k][j]);
					if (price < minp)
						minp = price;
				}
				bud-=minp;
				if (bud < 0) return ans;
			}
		}
		return n;
						
	}
};


// BEGIN KAWIGIEDIT TESTING
// Generated by KawigiEdit-pf 2.3.0
#include <iostream>
#include <string>
#include <vector>
#include <ctime>
#include <cmath>
using namespace std;
bool KawigiEdit_RunTest(int testNum, vector <string> p0, int p1, bool hasAnswer, int p2) {
	cout << "Test " << testNum << ": [" << "{";
	for (int i = 0; int(p0.size()) > i; ++i) {
		if (i > 0) {
			cout << ",";
		}
		cout << "\"" << p0[i] << "\"";
	}
	cout << "}" << "," << p1;
	cout << "]" << endl;
	MysteriousRestaurant *obj;
	int answer;
	obj = new MysteriousRestaurant();
	clock_t startTime = clock();
	answer = obj->maxDays(p0, p1);
	clock_t endTime = clock();
	delete obj;
	bool res;
	res = true;
	cout << "Time: " << double(endTime - startTime) / CLOCKS_PER_SEC << " seconds" << endl;
	if (hasAnswer) {
		cout << "Desired answer:" << endl;
		cout << "\t" << p2 << endl;
	}
	cout << "Your answer:" << endl;
	cout << "\t" << answer << endl;
	if (hasAnswer) {
		res = answer == p2;
	}
	if (!res) {
		cout << "DOESN'T MATCH!!!!" << endl;
	} else if (double(endTime - startTime) / CLOCKS_PER_SEC >= 2) {
		cout << "FAIL the timeout" << endl;
		res = false;
	} else if (hasAnswer) {
		cout << "Match :-)" << endl;
	} else {
		cout << "OK, but is it right?" << endl;
	}
	cout << "" << endl;
	return res;
}
int main() {
	bool all_right;
	bool disabled;
	bool tests_disabled;
	all_right = true;
	tests_disabled = false;
	
	vector <string> p0;
	int p1;
	int p2;
	
	{
	// ----- test 0 -----
	disabled = false;
	string t0[] = {"26","14","72","39","32","85","06"};
			p0.assign(t0, t0 + sizeof(t0) / sizeof(t0[0]));
	p1 = 13;
	p2 = 5;
	all_right = (disabled || KawigiEdit_RunTest(0, p0, p1, true, p2) ) && all_right;
	tests_disabled = tests_disabled || disabled;
	// ------------------
	}
	
	{
	// ----- test 1 -----
	disabled = false;
	string t0[] = {"26","14","72","39","32","85","06","91"};
			p0.assign(t0, t0 + sizeof(t0) / sizeof(t0[0]));
	p1 = 20;
	p2 = 8;
	all_right = (disabled || KawigiEdit_RunTest(1, p0, p1, true, p2) ) && all_right;
	tests_disabled = tests_disabled || disabled;
	// ------------------
	}
	
	{
	// ----- test 2 -----
	disabled = false;
	string t0[] = {"SRM","512"};
			p0.assign(t0, t0 + sizeof(t0) / sizeof(t0[0]));
	p1 = 4;
	p2 = 0;
	all_right = (disabled || KawigiEdit_RunTest(2, p0, p1, true, p2) ) && all_right;
	tests_disabled = tests_disabled || disabled;
	// ------------------
	}
	
	{
	// ----- test 3 -----
	disabled = false;
	string t0[] = {"Dear","Code","rsHa","veFu","nInT","heCh","alle","ngeP","hase","andb","ecar","eful"};
			p0.assign(t0, t0 + sizeof(t0) / sizeof(t0[0]));
	p1 = 256;
	p2 = 10;
	all_right = (disabled || KawigiEdit_RunTest(3, p0, p1, true, p2) ) && all_right;
	tests_disabled = tests_disabled || disabled;
	// ------------------
	}
	
	if (all_right) {
		if (tests_disabled) {
			cout << "You're a stud (but some test cases were disabled)!" << endl;
		} else {
			cout << "You're a stud (at least on given cases)!" << endl;
		}
	} else {
		cout << "Some of the test cases had errors." << endl;
	}
	return 0;
}
// PROBLEM STATEMENT
// A mysterious new restaurant is open in the city for only N days. Happy to hear that, Ash and Elsh would like to have lunch at the restaurant on as many days as possible.
// 
// The restaurant sells M types of dishes. Being a mysterious restaurant, it has mysterious rules for the customers:
// 
// 
// They can only buy one single dish per day.
// If they buy a dish of type j on the i-th day, then on the same day next week, i.e., on the (i+7)-th day, they can only buy a dish of type j.
// If they don't buy any dishes on any day, then they can't buy any dishes again from the restaurant.
// 
// 
// Mysteriously, the price of each type of dish varies every day. You are given a vector <string> prices consisting of N elements, each containing M characters. prices[i][j] represents the price of the j-th type of dish on the i-th day, encoded as follows:
// 
// 
// '0' - '9': denotes the price of 0 - 9 dollars.
// 'A' - 'Z': denotes the price of 10 - 35 dollars.
// 'a' - 'z': denotes the price of 36 - 61 dollars.
// 
// 
// Ash and Elsh have only budget dollars allocated for having lunch in the restaurant. Return the maximum number of days they could have lunch in the restaurant.
// 
// DEFINITION
// Class:MysteriousRestaurant
// Method:maxDays
// Parameters:vector <string>, int
// Returns:int
// Method signature:int maxDays(vector <string> prices, int budget)
// 
// 
// CONSTRAINTS
// -prices will contain between 1 and 50 elements, inclusive.
// -Each element of prices will contain the same number of characters, between 1 and 50 characters, inclusive.
// -Each character in prices will be '0'-'9', 'a'-'z', or 'A'-'Z'.
// -budget will be between 0 and 10,000, inclusive.
// 
// 
// EXAMPLES
// 
// 0)
// {"26", "14", "72", "39", "32", "85", "06"}
// 13
// 
// Returns: 5
// 
// The restaurant is open for 7 days. They can have lunch for 5 days, each picking the cheaper dish from the two available types. The total prices would be 2+1+2+3+2 = 10.
// 
// 1)
// {"26", "14", "72", "39", "32", "85", "06", "91"}
// 20
// 
// Returns: 8
// 
// In this case, it is better to buy the second type of dish on the first and the eighth day, so they can have lunch for the entire 8 days.
// 
// 2)
// {"SRM", "512"}
// 4
// 
// Returns: 0
// 
// They don't have sufficient budget.
// 
// 3)
// {"Dear", "Code", "rsHa", "veFu", "nInT", "heCh", "alle", "ngeP", "hase", "andb", "ecar", "eful"}
// 256
// 
// Returns: 10
// 
// 
// 
// END KAWIGIEDIT TESTING
//Powered by KawigiEdit-pf 2.3.0!
